﻿using Humanizer.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Mysqlx.Crud;
using Mysqlx.Prepare;
using Quartz;
using SageKing.OpenIddict.UI.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SageKing.Extensions;

namespace SageKing.OpenIddict.UI.Infrastructure.Features;

public class OpenIddictUIInfrastructureFeature : FeatureBase
{
    public OpenIddictUIInfrastructureFeature(IModule module) : base(module)
    {
    }

    /// <summary>
    /// Represents the options for OpenIds feature.
    /// </summary>
    public Action<OpenIddictUIInfrastructureOptions> OpenIdOptionsAction { get; set; } = _ => { };
    public Action<IdentityOptions> IdentityOptionsAction { get; set; } = _ => { };
    Action<OpenIddictUIStoreOptions> storeOptionsAction { get; set; } = _ => { };

    /// <summary>
    /// 
    /// </summary>
    public OpenIddictUIInfrastructureOptions OpenIdOptions { get; set; } = new OpenIddictUIInfrastructureOptions();

    /// <inheritdoc />
    public override void Apply(IConfigurationManager configurationManager, IHostEnvironment hostEnvironment)
    {
        ArgumentNullException.ThrowIfNull(configurationManager);
         
        //configurationManager.Bind(OpenIdOptions);

        Services.Configure(OpenIdOptionsAction);
        Services.Configure(storeOptionsAction);
        Services.Configure(IdentityOptionsAction);

        Services.AddInfrastructureServices();

        var coreBuilder = new OpenIddictCoreBuilder(Services)
          .UseEFCoreUIStore<OpenIddictUIContext>();

        Services
           .AddOpenIddictUIStore<OpenIddictUIContext>(storeOptionsAction);

    } 

}
